package com.hdjk.view.activity.personalcenter;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import com.hdjk.view.R;
import com.hdjk.view.adapter.IntegralAdapter;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.entity.IntegralEntity;
import com.hdjk.view.network.PersonalNetWork;
import com.hdjk.view.util.EmptyViewUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.component.CustomView.TopTitleBar;
import com.lst.projectlib.entity.Msg;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.loadingviewfinal.DefaultLoadMoreView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;

/**
 * Created by sxy on 2017/11/1.
 */

public class IntegralListActivity extends BaseActivity implements View.OnClickListener {
    private List<IntegralEntity> mList;
    private IntegralAdapter mAdapter;
    private int mPageIndex = 0;

    // Content View Elements
    private TopTitleBar ttb_title_bar;
    private cn.finalteam.loadingviewfinal.PtrClassicFrameLayout ptr_layout;
    private cn.finalteam.loadingviewfinal.ListViewFinal lv_data_list;
    private FrameLayout mFlEmptyView;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (TopTitleBar) findViewById(R.id.ttb_title_bar);
        ptr_layout = (cn.finalteam.loadingviewfinal.PtrClassicFrameLayout) findViewById(R.id.ptr_layout);
        mFlEmptyView = (FrameLayout) findViewById(R.id.fl_empty_view);
        lv_data_list = (cn.finalteam.loadingviewfinal.ListViewFinal) findViewById(R.id.lv_data_list);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_integral_list);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        mFlEmptyView.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        mAdapter = new IntegralAdapter(this);
        mAdapter.setDataList(mList);

        lv_data_list.setLoadMoreView(new DefaultLoadMoreView(this));
        lv_data_list.setAdapter(mAdapter);
        lv_data_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        lv_data_list.setEmptyView(mFlEmptyView);

        ptr_layout.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // 下拉刷新
                getNetworkList(true);
            }
        });
        ptr_layout.setLastUpdateTimeRelateObject(this);
        lv_data_list.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                // 上拉加载
                getNetworkList(false);
            }
        });
        getNetworkList(true);
    }

    private void onLoaded() {
        ptr_layout.onRefreshComplete();
        lv_data_list.onLoadMoreComplete();
    }

    private void getNetworkList(final boolean isDropDown) {
        mDialog.show();
        if (isDropDown) {
            mPageIndex = 1;
        }
        PersonalNetWork.getIntegralList(UserTokenUtil.getToken(this), mPageIndex + "", new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                onLoaded();
                mDialog.dismiss();
                if (msg != null && msg.getSuccess() && msg.getData() != null) {
                    if (TextUtils.isEmpty(msg.getData().toString())) {
                        lv_data_list.setHasLoadMore(false);
                    } else {
                        List<IntegralEntity> tempList = (ArrayList) msg.getData();

                        if (null != tempList && tempList.size() > 0) {
                            mPageIndex += 1;

                            if (isDropDown) {
                                mList = tempList;
                            } else {
                                mList.addAll(tempList);
                            }

                            if (mAdapter == null) {
                                mAdapter = new IntegralAdapter(IntegralListActivity.this);
                                mAdapter.setDataList(mList);
                                lv_data_list.setAdapter(mAdapter);
                            } else {
                                mAdapter.setDataList(mList);
                                mAdapter.notifyDataSetChanged();
                            }
                            lv_data_list.setHasLoadMore(true);
                        }
                        if (null == tempList || (tempList.size() >= 0 && tempList.size() < 10)) {
                            lv_data_list.setHasLoadMore(false);
                        }
                    }
                }
                if (mList == null || mList.size() <= 0) {
                    EmptyViewUtils.showNoDataEmpty(mFlEmptyView);
                    return;
                }
            }

            @Override
            public void onCallBackError(Msg msg) {
                mDialog.cancel();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.fl_empty_view:
                getNetworkList(true);
                break;
        }
    }
}
